﻿using LituoPro.constant;
using LituoPro.util;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LituoPro.network
{
    public class NetWorkHandle
    {
        public static List<Byte[]> getNetWorkPackage(Byte[] inputBuf)
        {
            List<Byte[]> list = new List<Byte[]>();
            if (inputBuf == null || inputBuf.Length == 0)
            {
                return null;
            }
            else if (inputBuf.Length < SystemConstant.PACKAGE_LENGTH)
            {
                return null;
            }
            else if (inputBuf.Length == SystemConstant.PACKAGE_LENGTH)
            {
                list.Add(inputBuf);
                return list;
            }
            else if (inputBuf.Length > SystemConstant.PACKAGE_LENGTH && inputBuf.Length < SystemConstant.PACKAGE_LENGTH * 2)
            {
                byte[] firstArray = inputBuf.Take(SystemConstant.PACKAGE_LENGTH).ToArray();
                list.Add(firstArray);
                return list;
            }
            else if (inputBuf.Length == SystemConstant.PACKAGE_LENGTH * 2)
            {
                byte[] firstArray = inputBuf.Take(SystemConstant.PACKAGE_LENGTH).ToArray();
                byte[] secondArray = inputBuf.Skip(SystemConstant.PACKAGE_LENGTH).ToArray();
                list.Add(firstArray);
                ushort startByte = BitConverter.ToUInt16(secondArray, 0);
                if (startByte == SystemConstant.PACKAGE_START_BYTE_DATA) 
                {
                    list.Add(secondArray);
                }
                return list;
            }
            else if (inputBuf.Length > SystemConstant.PACKAGE_LENGTH * 2)
            {
                // 第一个截取后的数组长度
                int firstLength = SystemConstant.PACKAGE_LENGTH;
                // 第二个截取后的数组长度
                int secondLength = SystemConstant.PACKAGE_LENGTH;
                byte[] firstArray = new byte[firstLength];
                byte[] secondArray = new byte[secondLength];
                // 复制前半部分到firstArray
                Array.Copy(inputBuf, 0, firstArray, 0, firstLength);
                // 复制后半部分到secondArray
                Array.Copy(inputBuf, SystemConstant.PACKAGE_LENGTH, secondArray, 0, secondLength);
                list.Add(firstArray);
                ushort startByte = BitConverter.ToUInt16(secondArray, 0);
                if (startByte == SystemConstant.PACKAGE_START_BYTE_DATA)
                {
                    list.Add(secondArray);
                }
                return list;
            }
            else
            {
                return null;
            }
        }






    }
}
